﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bussiness;

namespace Presentation {
    public partial class frmLapHopDong : Form {
        private DataTable _tblProducts;
        private DataTable _tblTerms;

        public frmLapHopDong() {
            InitializeComponent();
        }

        private void frmLapHopDong_Load(object sender, EventArgs e) {
            //load all products to datatable
            SanPhamBussiness productBuss = new SanPhamBussiness();
            this._tblProducts = productBuss.getProducts();
            //init cbx
            cbxProducts.DisplayMember = "TenSP";
            cbxProducts.ValueMember = "MaSP";
            cbxProducts.DataSource = this._tblProducts;
            
        }

        private void btnSearch_Click(object sender, EventArgs e) {
            String termID = this.txtSearch.Text.Trim();
            KhachHangBussiness buss = new KhachHangBussiness();
            this._tblTerms = buss.getTermsAndRepresentatives(termID);
            
            grvTerms.DataSource = this._tblTerms;
            
        }

        private void btnAdd_Click(object sender, EventArgs e) {
            string id = cbxProducts.SelectedValue.ToString();
            String name = _tblProducts.Rows[cbxProducts.SelectedIndex]["TenSP"].ToString();
            string quanlity = txtQuanlity.Text;
            string price = (int.Parse(quanlity) * float.Parse(_tblProducts.Rows[cbxProducts.SelectedIndex]["DonGia"].ToString())).ToString();
            String[] row = new String[] { id, name, quanlity, price };
            grvProducts.Rows.Add(row);
        }

        private void btnCreate_Click(object sender, EventArgs e) {
            //String contractType, float price, String employeeID, String termID
            String contractType = comboBox1.Text;
            float price = 1;
            String employeeID = "CV        ";
            String termID = grvTerms.CurrentRow.Cells["MaCongTy"].Value.ToString();

            ContractBussiness contractBuss = new ContractBussiness();
            int id = contractBuss.insert(contractType, price, employeeID, termID, txtContract.Text);

            //add detail
            for (int i = 0; i < grvProducts.Rows.Count; i++) {
                int productID = int.Parse(grvProducts.Rows[i].Cells[0].Value.ToString());
                int quanlity = int.Parse(grvProducts.Rows[i].Cells[2].Value.ToString());
                String design = "no design";

                contractBuss.insertDetail(txtContract.Text, productID, quanlity, design);
            }

            MessageBox.Show("Them thanh cong");
        }
    }
}
